package com.wilmer.ys.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wilmer.common.annotation.Anonymous;
import com.wilmer.vo.LineVo;
import org.apache.commons.compress.utils.Lists;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.ys.domain.ChildYs;
import com.wilmer.ys.service.IChildYsService;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.common.core.page.TableDataInfo;

/**
 * 饮水量图表数据Controller
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/ys/ys")
public class ChildYsController extends BaseController
{
    @Autowired
    private IChildYsService childYsService;

    /**
     * 查询饮水量图表数据列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('ys:ys:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildYs childYs)
    {
        startPage();
        List<ChildYs> list = childYsService.selectChildYsList(childYs);
        return getDataTable(list);
    }

    /**
     * 导出饮水量图表数据列表
     */
    @PreAuthorize("@ss.hasPermi('ys:ys:export')")
    @Log(title = "饮水量图表数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildYs childYs)
    {
        List<ChildYs> list = childYsService.selectChildYsList(childYs);
        ExcelUtil<ChildYs> util = new ExcelUtil<ChildYs>(ChildYs.class);
        util.exportExcel(response, list, "饮水量图表数据数据");
    }

    /**
     * 获取饮水量图表数据详细信息
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('ys:ys:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(childYsService.selectChildYsById(id));
    }

    /**
     * 新增饮水量图表数据
     */
    @PreAuthorize("@ss.hasPermi('ys:ys:add')")
    @Log(title = "饮水量图表数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildYs childYs)
    {
        return toAjax(childYsService.insertChildYs(childYs));
    }

    /**
     * 修改饮水量图表数据
     */
    @PreAuthorize("@ss.hasPermi('ys:ys:edit')")
    @Log(title = "饮水量图表数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildYs childYs)
    {
        return toAjax(childYsService.updateChildYs(childYs));
    }

    /**
     * 删除饮水量图表数据
     */
    @PreAuthorize("@ss.hasPermi('ys:ys:remove')")
    @Log(title = "饮水量图表数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(childYsService.deleteChildYsByIds(ids));
    }

    /**
     * 查询每天孩子的饮水量
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('ys:ys:daily')")
    @GetMapping("/water")
    public LineVo ysChartsDaily(@Param("userId") Long userId, @Param("days") Long days)
    {
        //获取饮水量集合
        List<ChildYs> ysList = childYsService.ysChartsDaily(userId,days);

        //初始化Vo对象
        LineVo lineVo = new LineVo();
        lineVo.setXdata(Lists.newArrayList());
        lineVo.setSeries(Lists.newArrayList());

        ysList.forEach( ys->{
            lineVo.getXdata().add(formatDate(ys.getDatetime()));
            lineVo.getSeries().add(ys.getYs());
        });

        return lineVo;
    }

    /**
     * 根据父母id查询孩子的饮水量列表
     * @param userId
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('ys:ys:child')")
    @GetMapping("/child")
    public TableDataInfo getListByUserId(@Param("userId") Long userId){
        startPage();
        List<ChildYs> list = childYsService.getListByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 日期转换
     * @param date
     * @return
     */
    private String formatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }
}
